package com.wangzh.nio.crawler;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
    /**
     * 制造教据:
     * 获取男生名字:http://www.haoming8.cn/baobao/10881.html
     * 获取姚氏:https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0
     * 张议女生名字:http://www.haoming8.cn/baobao/7641.html
     *
     * @param args
     */
    public static void main(String[] args) throws IOException {

        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlyNameNet = "http://www.haoming8.cn/baobao/7641.html";


        String familyStr = webCrawler(familyNameNet);
        String boyStr = webCrawler(boyNameNet);
        String girlStr = webCrawler(girlyNameNet);

        //3通过正则表达式，把符合的数据返回
        ArrayList<String> familyList = getData(familyStr, "([^a-zA-Z]{4})(，|。)", 1);
        ArrayList<String> boyList = getData(boyStr, "([\\u4e00-\\u9fa5·]{2})(、|。)", 1);
        ArrayList<String> girlList = getData(girlStr, "(.. ){4}..", 0);
        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : familyList) {
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                familyNameList.add(c+"");
            }
        }
        ArrayList<String> boyNameList = new ArrayList<>();
        for (String s : boyList) {
            if (!boyNameList.contains(s)) {
                boyNameList.add(s);
            }
        }
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String names : girlList) {
            String[] name = names.split(" ");
            for (String s : name) {
                girlNameList.add(s);
            }
        }

        //生产数据
        List<String> list = getInfos(familyNameList, boyNameList, girlNameList, 70, 50);
        Collections.shuffle(list);

        BufferedWriter bw = new BufferedWriter(new FileWriter("names.txt",true));
        for (String name : list) {
            bw.write(name);
            bw.newLine();
        }
        bw.close();
        System.out.println(list);
    }

    private static List<String> getInfos(ArrayList<String> familyList, ArrayList<String> boyList, ArrayList<String> girlList, int boycount, int girlcount) {

        //男姓名
        HashSet<String> boyset = new HashSet<>();
        while (true) {
            if (boyset.size() >= boycount) {
                break;
            }
            Collections.shuffle(familyList);
            Collections.shuffle(boyList);
            boyset.add(familyList.get(0) + boyList.get(0));

        }

        //女生姓名
        HashSet<String> girlset = new HashSet<>();
        while (true) {
            if (girlset.size() >= girlcount) {
                break;
            }
            Collections.shuffle(familyList);
            Collections.shuffle(girlList);
            girlset.add(familyList.get(0) + girlList.get(0));

        }

        //组合姓名  张三-男-23
        List<String> hs = new ArrayList<>();
        Random r = new Random();
        for (String s : boyset) {
            int age = r.nextInt(10) + 18;
            hs.add(s + "-男-" + age);
        }

        for (String s : girlset) {
            int age = r.nextInt(10) + 15;
            hs.add(s + "-女-" + age);
        }


        return hs;
    }

    private static ArrayList<String> getData(String str, String regex, int index) {
        ArrayList<String> list = new ArrayList<>();
        //按照正则表达式的规则获取数据
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            String group = matcher.group(index);
            list.add(group);
//            System.out.println(group);
        }
        return list;
    }

    /**
     * 作用：
     * 从网络中爬取数据，把数据拼接成字符串
     *
     * @return
     */
    public static String webCrawler(String net) throws IOException {
        //1.定义StringBuilder拼接爬取到的数据
        StringBuilder sb = new StringBuilder();
        //2.
        URL url = new URL(net);
        //3.连接这个网址
        URLConnection conn = url.openConnection();

        InputStream inputStream = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(inputStream);
        int ch;
        while ((ch = isr.read()) != -1) {
            sb.append(((char) ch));
        }

        isr.close();

        return sb.toString();

    }
}
